package com.spirng.leetcode.day06;


import java.util.*;

public class Test145 {
    public static void main(String[] args) {
        String s = new StringBuilder().append("ja").append("va").toString();
        System.out.println(s.intern()==s);
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(node!=null){
                list.add(node.val);
            }else {
                continue;
            }
            stack.push(node.right);
            stack.push(node.left);
        }
        return list;
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        ArrayList<Integer> list = new ArrayList<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(node!=null){
                list.add(node.val);
            }else {
                continue;
            }
            stack.push(node.left);
            stack.push(node.right);

        }
        Collections.reverse(list);
        return list;
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        ArrayList<Integer> list = new ArrayList<>();
        TreeNode cur=root;
        while (cur!=null||!stack.isEmpty()){
            if (cur!=null){
                stack.push(cur);
                cur=cur.left;
            }else {
                TreeNode node = stack.pop();
                list.add(node.val);
                cur=node.right;
            }
        }
        return list;
    }
}
